﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Cms.Web.CmsSystem.SysModules.TabPage
{
    public partial class List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        Cms.BLL.TabPage bll = new Cms.BLL.TabPage();
        public string TreeJson
        {
            get
            {
                StringBuilder json = new StringBuilder();
                List<Model.Sys_Tabpage> list = bll.GetAll();
                foreach (Model.Sys_Tabpage model in list)
                {
                    /*if (String.IsNullOrEmpty(model.Parentid))
                    {
                        json.Append("{");
                        json.Append(BuildTreeJson(list, model.Id));
                        json.Append("},");
                    }*/
                    json.Append("{");
                    json.AppendFormat(" id: '{0}', pId: '{1}', name: '{2}',tabtype:'{3}'", model.Id, model.Parentid, model.Title, model.Tab_Type);
                    json.AppendLine("},");
                }
                return json.ToString().Trim(System.Environment.NewLine.ToCharArray()).Trim(',');
            }
        }/*
        public string BuildTreeJson(List<Model.Sys_Tabpage> list, string tabid)
        {
            List<Model.Sys_Tabpage> listChild = bll.GetChilds(list, tabid);

            StringBuilder sb = new StringBuilder();
            foreach (Model.Sys_Tabpage model in listChild)
            {
                sb.Append("{");
                sb.AppendFormat(" id: '{0}', pId: '{1}', name: '{2}',tabtype:'{3}'", model.Id, model.Parentid, model.Title, model.Tab_Type);
                sb.AppendLine("},");
                sb.Append(BuildTreeJson(list, model.Id));
            }
            return sb.ToString();

        }*/
        private static Dictionary<string, int> _wpttype = null;
        private static object obj = new object();
        private Dictionary<string, int> TabType
        {
            get
            {
                if (_wpttype == null)
                {
                    lock (obj)
                    {
                        _wpttype = new Dictionary<string, int>();
                        Type type = typeof(Cms.Model.Sys_Tabpagetype);
                        string[] members = Enum.GetNames(type);//type.GetNestedTypes();
                        foreach (string member in members)
                        {
                            _wpttype.Add(member, (int)type.GetField(member).GetRawConstantValue());
                            //sb.AppendFormat("<option value={1}>{0}</option>", member, type.GetField(member).GetRawConstantValue());
                        }
                    }
                }
                return _wpttype;
            }
        }
        public string JsTabtype
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                foreach (KeyValuePair<string, int> k in TabType)
                {
                    sb.AppendFormat("{0}:{1},", k.Key, k.Value);
                }
                return sb.ToString().Trim(',');
            }
        }

        public string Options
        {
            get
            {
                StringBuilder sb = new StringBuilder();

                foreach (KeyValuePair<string, int> k in TabType)
                {
                    sb.AppendFormat("<option value='{1}'>{0}</option>", k.Key, k.Value);
                }
                return sb.ToString();
            }
        }

        public string IconBuilder()
        {
            int size = 16;
            StringBuilder sb = new StringBuilder();
            sb.Append("<div id='IconPanel'>");
            string imgPath = Server.MapPath(String.Format("~/Images/Icon/{0}", size));
            if (System.IO.Directory.Exists(imgPath))
            {
                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(imgPath);
                System.IO.FileInfo[] fis = di.GetFiles();
                string imgShortPath = String.Format("Icon/{0}/", size);
                foreach (System.IO.FileInfo fi in fis)
                {
                    string fileName = fi.Name;
                    string shortName = fileName.Replace(fi.Extension, "");
                    if (EntLib.TypeParse.IsNumber(shortName))
                    {
                        shortName = "icon_" + shortName;
                    }
                    sb.AppendFormat("<div class='icon {0}'></div>", shortName);
                }
                sb.Append("<div style='clear:both;'></div>");
            }
            sb.Append("</div>");
            return sb.ToString();
        }
    }
}
